<template>
<div>

<div v-if="pageControll">

  <el-button @click="handleAdjustScheme1">返回</el-button>

  
</div>


  <div v-if="!pageControll" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工工号" prop="employeeNumber">
        <el-input
          v-model="queryParams.employeeNumber"
          placeholder="请输入员工工号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入职日期" prop="hireDate">
        <el-date-picker clearable
          v-model="queryParams.hireDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择入职日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
        
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['cbda:cbda:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>



  <div class="stats-container">
      <div>全部: {{ total }}</div>
      <div>投保中: {{ hasInsurancePlanCount }}</div>
      <div>待投保: {{ noInsurancePlanCount }}</div>
    </div>

    <el-table v-loading="loading" :data="cbdaList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工" align="center" prop="name" />
      <el-table-column label="工号" align="center" prop="employeeNumber" />
      <el-table-column label="员工部门" align="center" prop="deptName" />
      <el-table-column label="员工职位" align="center" prop="posName" />
      <el-table-column label="员工类型" align="center" prop="type" />
      <el-table-column label="员工状态" align="center" prop="status" />
      <el-table-column label="参保状态" align="center" prop="insurancePlanName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleInsurance (scope.row)" v-hasPermi="['cbda:cbda:remove']">投保</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参保档案对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cbdaRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>


  </div>
</template>

<script setup name="Cbda">
import { listCbda, getCbda, delCbda, addCbda, updateCbda } from "@/api/cbda/cbda"
import { useRouter } from "vue-router"; 
const router = useRouter();
const { proxy } = getCurrentInstance()

const cbdaList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const pageControll = ref(false)

const hasInsurancePlanCount = ref(0)
const noInsurancePlanCount = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    employeeNumber: null,
    hireDate: null,
  },
  rules: {
    name: [
      { required: true, message: "员工姓名不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 调整方案1 */
function handleAdjustScheme1() {
  pageControll.value = false; // 调整方案1的逻辑
}

/** 查询参保档案列表 */
function getList() {
  loading.value = true
  listCbda(queryParams.value).then(response => {
    cbdaList.value = response.rows
    total.value = response.total

      if (response.rows.length > 0) {
      hasInsurancePlanCount.value = response.rows[0].hasInsurancePlanCount
    } else {
      hasInsurancePlanCount.value = 0
    }

    // 修正获取第一个对象的 noInsurancePlanCount 字段的方式
    if (response.rows.length > 0) {
      noInsurancePlanCount.value = response.rows[0].noInsurancePlanCount
    } else {
      noInsurancePlanCount.value = 0
    }


    loading.value = false
  })
}



// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    avatar: null,
    name: null,
    employeeNumber: null,
    departmentId: null,
    phone: null,
    positionId: null,
    type: null,
    idNumber: null,
    email: null,
    hireDate: null,
    certificateId: null,
    contractId: null,
    gender: null,
    status: null,
    insurancePlanId: null,
    delFlag: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("cbdaRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加参保档案"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getCbda(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改参保档案"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cbdaRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCbda(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addCbda(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

function handleInsurance(row) {
  console.log('row.insurancePlanId:', row.insurancePlanId);
  if (!row.insurancePlanId) {
    router.push({
      path: "/tb",
      // 传递当前行的工号
      query: { 
        insurancePlanId: '',
        employeeNumber: row.employeeNumber 
      }, 
    }).catch(error => {
      console.error('路由跳转出错:', error); 
      proxy.$modal.msgError('页面跳转失败，请稍后重试');
    });
  } else {
    proxy.$modal.msgWarning("已获取到参保计划 ID，无需跳转");
  }
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cbda/cbda/export', {
    ...queryParams.value
  }, `cbda_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<style scoped>
.stats-container {
  background-color: #f8f9fa; /* 背景颜色 */
  border: 1px solid #dee2e6; /* 边框 */
  border-radius: 4px; /* 圆角 */
  padding: 10px; /* 内边距 */
  margin-bottom: 10px; /* 底部外边距 */
  display: flex; /* 使用 Flex 布局 */
  justify-content: space-around; /* 子元素均匀分布 */
  flex-wrap: wrap; /* 允许子元素换行 */
}

.stats-container > div {
  padding: 5px 10px; /* 子元素内边距 */
  font-size: 14px; /* 字体大小 */
  color: #333; /* 字体颜色 */
}
</style>
